package com.dianjing.dianjing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dianjing.dianjing.dto.input.Evaluate;
import com.dianjing.dianjing.dto.input.OrderInput;
import com.dianjing.dianjing.entity.Demand;
import com.dianjing.dianjing.entity.Order;
import com.dianjing.dianjing.entity.Painting;
import com.dianjing.dianjing.entity.User;
import com.dianjing.dianjing.enums.OrderOpenStateEnum;
import com.dianjing.dianjing.enums.OrderStateEnum;
import com.dianjing.dianjing.enums.UserTypeEnum;
import com.dianjing.dianjing.enums.DemandStateEnum;
import com.dianjing.dianjing.mapper.OrderMapper;
import com.dianjing.dianjing.service.DemandService;
import com.dianjing.dianjing.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dianjing.dianjing.service.UserService;
import com.dianjing.dianjing.util.DataUtils;
import com.dianjing.dianjing.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author LBG
 * @since 2022-11-09
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Resource
    private DemandService demandService;

    @Autowired
    private UserService userService;


    /**
     * 接受需求生成订单
     *
     * @param input
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public DataUtils receiveDemand(OrderInput input) {
        // 改需求状态
        Demand demand = new Demand();
        demand.setId(input.getDemandId());
        demand.setState(DemandStateEnum.ACCEPTED.getState());
        demandService.updateById(demand);
        // 订单入库
        Order map = MapUtil.map(input, Order.class);
        map.setDdl(new Date());
        this.save(map);
        return DataUtils.ok();
    }

    /**
     * 修改订单状态
     *
     * @param input
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public DataUtils updateOrderState(OrderInput input) {
        this.updateById(MapUtil.map(input,Order.class));
        return DataUtils.ok();
    }



    @Override
    public Boolean createCompleted(Long painterId, Long orderId) {
        // 首先painterId 对应的用户必须是画师，其次订单的对应的画师是自己，且状态是“待创作”
        User painter = userService.getById(painterId);
        Order order = baseMapper.selectById(orderId);
        if(painter.getType() == UserTypeEnum.PAINTER.getType()
                && painterId.equals(order.getPainterId())
                && order.getState().equals(OrderStateEnum.WAIT_CREATE.getState()) ) {
            // 满足创作完成条件 - 理论上需要上传作品
            // 修改订单状态为“待确认”
            order.setState(OrderStateEnum.WAIT_CONFIRM.getState());
            baseMapper.updateById(order);
            return true;
        }
        return false;
    }

    @Override
    public List<Order> showStateOrderList(Long userId,Integer state) {
        User user = userService.getById(userId);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        if( user.getType() == UserTypeEnum.PAINTER.getType()) {
            // 身份为画师-获取订单的画师id为自己, 状态为指定状态
            wrapper.eq("painter_id",userId).eq("state", state);
        }else if( user.getType() == UserTypeEnum.DEMANDER.getType()) {
            // 身份为需求方-获取订单的需求访id为自己，状态为指定状态
            wrapper.eq("demand_user_id",userId).eq("state",state);
        }
        return baseMapper.selectList(wrapper);
    }

    @Override
    public Boolean confirmAndEvaluate(Long demanderId, Long orderId, Evaluate evaluate) {
        // 根据orderId查询订单的详情
        Order order = baseMapper.selectById(orderId);

        // 判断订单的需求方是否为 demanderId && 判断订单状态是否为待确认
        if (order.getDemandUserId().equals(demanderId)
                && order.getState().equals(OrderStateEnum.WAIT_CONFIRM.getState())) {
            // 修改订单状态为已完成且修改订单的评价信息
            order.setEvaluate(evaluate.getEvaluate());
            order.setStarLevel(evaluate.getStarLevel());
            // 设置订单为“已完成”
            order.setState(OrderStateEnum.COMPLETED.getState());
            int i = baseMapper.updateById(order);
            if(i > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Boolean openShow(Long orderId) {
        // 修改订单为公开
        Order order = baseMapper.selectById(orderId);
        order.setOpen(OrderOpenStateEnum.OPEN.getValue());
        return  baseMapper.updateById(order) > 0;
    }


}
